package org.com.blbl.Province;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class ProvinceDriver {
    public static void main(String[] args) throws Exception {

        //配置文件对象
        Configuration conf = new Configuration();
        //构造job作业的实例，参数（配置对象，job名）
        Job job = Job.getInstance(conf, ProvinceDriver.class.getSimpleName());

        //设置作业驱动类
        job.setJarByClass(ProvinceDriver.class);

        // 设置作业mapper reducer类
        job.setMapperClass(ProvinceMapper.class);
        job.setReducerClass(ProvinceReducer.class);

        // 设置作业mapper阶段输出key value数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置作业reducer阶段输出key value数据类型 也就是程序最终输出数据类型

        //配置本次作业的输入数据路径和输出数据路径
        Path inputPath = new Path("input/bilibilib_user.csv");
        Path outputPath = new Path("output/Province");

        FileInputFormat.setInputPaths(job, inputPath);
        FileOutputFormat.setOutputPath(job,outputPath);

        //todo 判断输出路径是否已经存在，如果已经存在，先删除
        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(outputPath)){
            fs.delete(outputPath,true); //递归删除
        }
        //最终提交本次job作业
        //job.submit();
        //采用waitForCompletion提交job，参数表示是否开启实时监视追踪作业的执行情况
        boolean resultFlag = job.waitForCompletion(true);
        //退出程序 和job结果进行绑定, 0是正常退出，1是异常退出
        System.exit(resultFlag ? 0: 1);

    }
}
